package cn.edu.scau.dormitory_management_system.module.examine.service.impl;

import cn.edu.scau.dormitory_management_system.enums.ApplicationStatusEnum;
import cn.edu.scau.dormitory_management_system.enums.RoleEnum;
import cn.edu.scau.dormitory_management_system.module.assign_dormitory.service.AssignDormitory;
import cn.edu.scau.dormitory_management_system.module.examine.entity.*;
import cn.edu.scau.dormitory_management_system.module.examine.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author ：Vizzk
 * @description：TODO
 * @date ：2021/8/19 18:05
 */
@Service("dormAdjustApplyService")
public class DormAdjustApplyServiceImpl implements DormAdjustApplyService {
    @Autowired
    private DormAdjustApplicationService dormAdjustService;

    @Autowired
    private FlowService flowService;

    @Autowired
    private FlowNodeService flowNodeService;

    @Autowired
    private DormAdjustAuditService dormAdjustAuditService;

    @Autowired
    private AssignDormitory assignDormitory;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyDormAdjust(DormAdjustApplicationEntity dormAdjustApplicationEntity) {
        try{
            FlowEntity flowEntity = new FlowEntity();
            flowEntity.setRemark("调宿申请");
            flowService.insertFlow(flowEntity);

            /** 创建提交审核节点 */
            FlowNodeEntity nodeEntity = new FlowNodeEntity();
            nodeEntity.setRoleId(RoleEnum.STUDENT.getRoleId());
            nodeEntity.setFlowId(flowEntity.getFlowId());
            nodeEntity.setFlowNodeName("提交审核");
            flowNodeService.insertFlowNode(nodeEntity);

            /** 创建学院审核节点 */
            FlowNodeEntity deptNodeEntity = new FlowNodeEntity();
            deptNodeEntity.setRoleId(RoleEnum.DEPT_MANAGER.getRoleId());
            deptNodeEntity.setFlowId(flowEntity.getFlowId());
            deptNodeEntity.setFlowNodeName("学院审核");
            flowNodeService.insertFlowNode(deptNodeEntity);

            /** 创建学校审核节点 */
            FlowNodeEntity collegeNode = new FlowNodeEntity();
            collegeNode.setRoleId(RoleEnum.COLLEGE_MANAGER.getRoleId());
            collegeNode.setFlowNodeName("学校审核");
            collegeNode.setFlowId(flowEntity.getFlowId());
            flowNodeService.insertFlowNode(collegeNode);

            /** 创建审核结束节点 */
            FlowNodeEntity endNode = new FlowNodeEntity();
            endNode.setRoleId(RoleEnum.NONE.getRoleId());
            endNode.setFlowNodeName("结束");
            endNode.setFlowId(flowEntity.getFlowId());
            flowNodeService.insertFlowNode(endNode);

            /** 连接各节点节点下一节点为学院审核 */
            nodeEntity.setNextNodeId(deptNodeEntity.getFlowNodeId());
            deptNodeEntity.setNextNodeId(collegeNode.getFlowNodeId());
            collegeNode.setNextNodeId(endNode.getFlowNodeId());
            flowNodeService.updateById(nodeEntity);
            flowNodeService.updateById(deptNodeEntity);
            flowNodeService.updateById(collegeNode);

            /** 设置流程当前所处节点 */
            flowEntity.setCurNodeId(deptNodeEntity.getFlowNodeId());
            flowService.updateById(flowEntity);

            /** 补充流程信息 */
            dormAdjustApplicationEntity.setFlowId(flowEntity.getFlowId());
            dormAdjustApplicationEntity.setStatus(ApplicationStatusEnum.DEPT_AUDIT.getStatus());
            dormAdjustService.insertDormAdjustApplication(dormAdjustApplicationEntity);

        } catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertDeptAudit(DormAdjustAuditEntity auditEntity) {
        try {
            /** 插入审核表 */
            auditEntity.setAuditDate(new Date());

            /** 获取调宿申请表 */
            DormAdjustApplicationEntity dormAdjustApplicaton = dormAdjustService.getById(auditEntity.getAdjustId());
            /** 获取对应流程 */
            FlowEntity flow = flowService.getById(dormAdjustApplicaton.getFlowId());
            auditEntity.setFlowNodeId(flow.getCurNodeId());

            /** 审核通过 */
            if(auditEntity.isPassed()){
                /** 更新申请表状态为学校审核 */
                dormAdjustApplicaton.setStatus(ApplicationStatusEnum.COLLEGE_AUDIT.getStatus());

                /** 获取学校审核节点并更新流程当前节点为学校节点 */
                FlowNodeEntity deptNode = flowNodeService.getById(auditEntity.getFlowNodeId());
                flow.setCurNodeId(deptNode.getNextNodeId());
                flowService.updateById(flow);
            }/** 审核未通过 */
            else{
                dormAdjustApplicaton.setStatus(ApplicationStatusEnum.FAIL.getStatus());
            }
            /** 插调审核表并更新申请表 */
            dormAdjustService.updateById(dormAdjustApplicaton);
            dormAdjustAuditService.insertDormAdjustAudit(auditEntity);
        } catch (Exception e){
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCollegeAudit(DormAdjustAuditEntity auditEntity) {
        try {
            /** 插入审核表 */
            auditEntity.setAuditDate(new Date());

            /** 获取调宿申请表 */
            DormAdjustApplicationEntity dormAdjustApplicaton = dormAdjustService.getById(auditEntity.getAdjustId());
            /** 获取学校审核节点及流程节点 */
            FlowEntity flow = flowService.getById(dormAdjustApplicaton.getFlowId());
            auditEntity.setFlowNodeId(flow.getCurNodeId());
            FlowNodeEntity collegeNode = flowNodeService.getById(auditEntity.getFlowNodeId());

            /** 审核通过 */
            if(auditEntity.isPassed()){
                /** 更新申请表状态为通过审核 */
                dormAdjustApplicaton.setStatus(ApplicationStatusEnum.PASSED.getStatus());
                /** 更新流程最后的节点为结束节点 */
                flow.setCurNodeId(collegeNode.getNextNodeId());
                flowService.updateById(flow);

                /** 退宿 */
                DormAdjustApplicationEntity dormAdjustApplicationEntity = dormAdjustService.getById(auditEntity.getAdjustId());
                assignDormitory.updateDormitory(dormAdjustApplicationEntity.getStuId(), dormAdjustApplicationEntity.getNewDormId());

            }/** 审核未通过 */
            else{
                dormAdjustApplicaton.setStatus(ApplicationStatusEnum.FAIL.getStatus());
            }
            /** 插入审核表并更新申请表 */
            dormAdjustService.updateById(dormAdjustApplicaton);
            dormAdjustAuditService.insertDormAdjustAudit(auditEntity);
        } catch (Exception e){
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void retreatApplication(Integer id) {
        try {
            DormAdjustApplicationEntity adjustApplication = dormAdjustService.getById(id);
            FlowEntity flow = flowService.getById(adjustApplication.getFlowId());

            /** 删除相关流程节点 */
            QueryWrapper<FlowNodeEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("flow_id",flow.getFlowId());
            flowNodeService.remove(wrapper);
            /** 删除相关流程 */
            flowService.removeById(flow.getFlowId());

            /** 删除相关审核流程 */
            dormAdjustAuditService.deleteByAdjustId(id);
            /** 删除申请表 */
            dormAdjustService.retreatApplication(id);
        } catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
